home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Snippets / MMapp / MMovieView.cp < prev    next >
Encoding:
Text File  |  1996-10-27  |  6.3 KB  |  288 lines  |  [TEXT/CWIE]

  1. #include "MMovieView.h"
  2. // MMovieView.cp
  3. // This class gets a string from the CPPb resource to use as the file name of the movie
  4. // then it opens and plays the movie upon a message from the play button.
  5. //  Sorry it's not better documented.  I had many re-writes before this one, and sometimes
  6. //  I don't know what something actually does, I just know the compiler buys it and it works =)
  7.  
  8.  
  9.  
  10. #include <Movies.h>
  11. #include <Gestalt.h>
  12. #include <GestaltEqu.h>
  13. #include <LString.h>
  14. #include <UDrawingState.h>
  15. #include <UGraphicsUtilities.h>
  16. #include <Quickdraw.h>
  17.  
  18.  
  19.  
  20. /* the function that makes the pane work with the button */
  21. void
  22. MMovieView::ListenToMessage( MessageT theMessage, void *ioParam )
  23. {
  24.     #pragma unused ( ioParam )
  25.     
  26.     switch( theMessage )
  27.     {
  28.         case msg_PlayMovie:
  29.             {
  30.                 
  31.                 if( FileExists != true )
  32.                 {
  33.                     theMovie = GetMovieFile();
  34.                     
  35.                     if( theMovie != nil )
  36.                     {
  37.                         SetMovie();
  38.                         PlayMovie();
  39.                         
  40.                         // display the first frame
  41.                         ::GoToBeginningOfMovie( theMovie );
  42.                         ::ShowMoviePoster( theMovie );
  43.                     }
  44.                 }
  45.                 else
  46.                     if( FileExists )
  47.                     {
  48.                         PlayMovie();
  49.                         
  50.                         // display the first frame
  51.                         ::GoToBeginningOfMovie( theMovie );
  52.                         ::ShowMoviePoster( theMovie );
  53.                         
  54.                         
  55.                         ::DisposeMovie( theMovie );
  56.                         
  57.                         theMovie = nil;
  58.                         FileExists = false; // set this so that it will get a new file
  59.                     }
  60.                         
  61.                 break;
  62.             }
  63.     }
  64. }
  65.  
  66.  
  67.  
  68. MMovieView *
  69. MMovieView::CreateMoviePaneStream(LStream *inStream )
  70. {
  71.     
  72.     return (new MMovieView( inStream ));
  73. }
  74.  
  75.  
  76. // ---------------------------------------------------------------------------------
  77. //        • MMovieView
  78. // ---------------------------------------------------------------------------------
  79.  
  80. MMovieView::MMovieView()
  81. {
  82.     mMovieTitle = gEmptyString;
  83. }
  84.  
  85.  
  86. MMovieView::MMovieView( LStream *inStream )
  87.         : LView( inStream )
  88. {
  89.     /* read in the Pascal String from the CPPb resource */
  90.     inStream->ReadPString( mMovieTitle );
  91.     
  92. }
  93.  
  94.  
  95. // ---------------------------------------------------------------------------------
  96. //        • ~MMovieView
  97. // ---------------------------------------------------------------------------------
  98.  
  99. MMovieView::~MMovieView()
  100. {
  101.     ::DisposeMovie( theMovie );
  102.     ::CloseMovieFile( movieResRef );
  103.     FileOpen = false;
  104.     ::ExitMovies();
  105. }
  106.  
  107. StringPtr
  108. MMovieView::GetMovieTitle ( Str255    outTitle ) const
  109. {
  110.  
  111.     return LString::CopyPStr ( mMovieTitle, outTitle );
  112.  
  113. }
  114.  
  115. // ---------------------------------------------------------------------------------
  116. //        • DrawSelf
  117. // ---------------------------------------------------------------------------------
  118.  
  119. void
  120. MMovieView::DrawSelf()
  121. {
  122.     // Save and normalize the color and pen states.
  123.     StColorPenState savePenState;
  124.     StColorPenState::Normalize();
  125.     
  126.     // Calculate the frame rect.
  127.     Rect    theFrame;
  128.     CalcLocalFrameRect( theFrame );
  129.     
  130.     if( FileExists != true )
  131.     {
  132.         theMovie = GetMovieFile(); /* if no file, we need to get one */
  133.         if( theMovie != nil )
  134.         {
  135.             SetMovie();        /* set the movie and the MovieView frame size */
  136.             FileExists = true; /* kewel, we got one ! */
  137.         }
  138.     }
  139.     else        // if this else isn't here, it erases itself on the next call to DrawSelf()
  140.         {
  141.             if( FileExists )
  142.             {
  143.                 ::GoToBeginningOfMovie( theMovie );
  144.                 ::ShowMoviePoster( theMovie );
  145.             }
  146.         }
  147. }
  148.  
  149.     
  150. Movie
  151. MMovieView::GetMovieFile( void )
  152. {
  153.     FSSpec                    movieSpec;
  154.     Str255                    movieFileName;
  155.     long                    qtVers;
  156.     short                    refNum = 0;
  157.     long                    dirNum = 0;
  158.     OSErr                    err;
  159.     
  160.     GetMovieTitle( movieFileName );
  161.     
  162.     try {
  163.     
  164.     if( (::Gestalt( gestaltQuickTime, &qtVers )) == noErr )
  165.     {
  166.         
  167.         /* this is the function that gets the movie FSSpec with the Str255 file name */
  168.         err = ::FSMakeFSSpec( refNum, dirNum, movieFileName, &movieSpec );
  169.         if( err == noErr )
  170.         {
  171.             ::EnterMovies();
  172.             
  173.             if( FileOpen != true )
  174.             {
  175.                 err = ::OpenMovieFile( &movieSpec, &movieResRef, fsRdPerm );
  176.                     if( err == noErr )
  177.                     {
  178.                         FileOpen = true;
  179.                         
  180.                         err = ::NewMovieFromFile( &theMovie, movieResRef, nil, nil, 
  181.                                                 newMovieActive, nil );
  182.                         if( err == noErr )
  183.                         {
  184.                             return theMovie;
  185.                         }
  186.                     }
  187.             }
  188.             else  /* if the file's already open, why open it again ? */
  189.                 {
  190.                     err = ::NewMovieFromFile( &theMovie, movieResRef, nil, nil, 
  191.                                                 newMovieActive, nil );
  192.                     if( err == noErr )
  193.                     {
  194.                         return theMovie;
  195.                     }
  196.                 }
  197.         }
  198.     }
  199.     else
  200.         {
  201.             /* oops something went wrong, trash the file */
  202.             ::DisposeMovie( theMovie );
  203.             ::CloseMovieFile( movieResRef );
  204.             
  205.             FileOpen = false;
  206.             FileExists = false;
  207.             theMovie = nil;
  208.             
  209.             return theMovie;
  210.         }
  211.         } // end try
  212.     
  213.     catch(...)
  214.     {    /* oops something went wrong, trash the file */
  215.         if( movieResRef )
  216.         {
  217.             ::DisposeMovie( theMovie );
  218.             ::CloseMovieFile( movieResRef );
  219.             
  220.             FileOpen = false;
  221.             FileExists = false;
  222.             theMovie = nil;
  223.             
  224.             return theMovie;
  225.         }
  226.     }
  227.     return theMovie; /* take us home */
  228. }
  229.  
  230. void
  231. MMovieView::SetMovie( void )
  232. {
  233.     short movieHeight;
  234.     short movieWidth;
  235.     short paneHeight;
  236.     short paneWidth;
  237.     
  238.     Rect    movieBounds, frame;
  239.     
  240.     /* heres something interesting: we get the size of the movie and */
  241.     /* then set our MMovieView pane to the size of the movie */
  242.     /* something to work on... need a way to center the resulting frame in */
  243.     /* the frames immediate surroundings ( immediate supervisor ? ) */
  244.     
  245.     
  246.     /* get the movie Rect size and set up the offset and GWorld */
  247.     ::GetMovieBox( theMovie, &movieBounds );
  248.     ::OffsetRect( &movieBounds, -movieBounds.left, -movieBounds.top );
  249.     ::SetMovieGWorld( theMovie, (CGrafPtr) GetMacPort(), nil );
  250.     
  251.     /* determine the movie size in a way that can be used by ResizeFrameBy() */
  252.     movieHeight = ( movieBounds.bottom - movieBounds.top );
  253.     movieWidth = ( movieBounds.right - movieBounds.left );
  254.     
  255.     /* get the current MovieView Rect size */
  256.     CalcLocalFrameRect( frame );
  257.     paneHeight = ( frame.bottom - frame.top );
  258.     paneWidth = ( frame.right - frame.left );
  259.     
  260.     /* resize the current MovieView Rect to the orig movie size */
  261.     ResizeFrameBy( (movieWidth - paneWidth), (movieHeight - paneHeight), 1 );
  262.     CalcLocalFrameRect( frame );
  263.     FocusDraw();
  264.     
  265. }
  266.  
  267. void
  268. MMovieView::PlayMovie( void )
  269. {
  270.     /* simple play function for QuickTime */
  271.     /* might be better to redefine this as an LPeriodical SpendTime() or something */
  272.     Rect frame;
  273.     CalcLocalFrameRect( frame );
  274.     FocusDraw();
  275.     
  276.     ::GoToBeginningOfMovie( theMovie );
  277.     
  278.     ::MoviesTask( theMovie, 0 );
  279.     
  280.     ::StartMovie( theMovie );
  281.     
  282.     while(( ::IsMovieDone( theMovie ) == false ))
  283.     {
  284.         ::MoviesTask( theMovie, 0 ); /* keep playing until finished */
  285.     }
  286. }
  287.     
  288.